//
//  ViewController.m
//  瀑布流
//
//  Created by 朱永超 on 2017/3/14.
//  Copyright © 2017年 zyc. All rights reserved.
//

#import "ViewController.h"
#import "YCCollectionViewLayout.h"
#import "YCCollectionViewCell.h"
#import "Model.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,YCCollectionViewLayoutdelegate>
{
    UICollectionView *_collectionView;
    NSMutableArray *_showArr;
}
@property(nonatomic , strong) NSMutableArray *plistArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置标题
    self.title = @"瀑布流";
    //设置页面颜色
    self.view.backgroundColor = [UIColor whiteColor];
    //创建自定义网格样式
    YCCollectionViewLayout *ycLayout = [YCCollectionViewLayout layoutWithCloumnCount:3];
    //赋值边距
    [ycLayout setCloumnSpacingWidth:10 rowSpacingHeight:10 borderSet:UIEdgeInsetsMake(10, 10, 10, 10)];
    ycLayout.delegate = self;
    
    //初始化网格
    _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:ycLayout];
    //签订协议
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //网格背景颜色
    _collectionView.backgroundColor = [UIColor orangeColor];
    //添加到视图
    [self.view addSubview:_collectionView];
    
    //初始化数组
    _showArr = [NSMutableArray array];
    
    //注册网格单元格
    [_collectionView registerClass:[YCCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    //设置导航右视图
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"ADD" style:UIBarButtonItemStylePlain target:self action:@selector(add)];
}
-(NSMutableArray *)plistArr
{
    if (!_plistArr) {
        _plistArr = [NSMutableArray array];
    }
    //plist文件数据添加到model   把model存到数组_plistArr
    NSString *path = [[NSBundle mainBundle] pathForResource:@"photos" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *arr = dic[@"content"];
    for (NSDictionary *dic in arr) {
        Model *model = [Model setDic:dic];
        [_plistArr addObject:model];
    }
    return _plistArr;
}
//添加方法
-(void)add
{
    if (_showArr.count < self.plistArr.count) {
        [_showArr addObject:self.plistArr[_showArr.count]];
        [_collectionView reloadData];
    }
}
//代理方法   算item高度的代理
-(CGFloat)allocCollectionViewLayout:(YCCollectionViewLayout *)layout itemHeightForWidth:(CGFloat)itemWidth with:(NSIndexPath *)indexPath
{
    Model *model = _showArr[indexPath.item];
    return model.height/model.width*itemWidth;
}
//网格行数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _showArr.count;
}
//单元格内容
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Model *mo = _showArr[indexPath.item];
    cell.model = mo;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
